//
//  photoesViewController.m
//  XWZ_News_Work
//
//  Created by dllo on 16/1/18.
//  Copyright © 2016年 dllo. All rights reserved.
//

#import "photoesViewController.h"
#import "PhotoCollectionViewCell.h"
#import "photoModel.h"

@interface photoesViewController ()<UICollectionViewDelegate, UICollectionViewDataSource>
@property (nonatomic, strong) UICollectionView *collectionV;
@property (nonatomic, strong) NSMutableArray *photoArr;
@property (nonatomic, strong) UILabel *label;
@end

@implementation photoesViewController

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    self.tabBarController.tabBar.hidden = YES;
}

- (void)viewWillDisappear:(BOOL)animated
{
    [super viewDidDisappear:animated];
    self.tabBarController.tabBar.hidden = NO;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor blackColor];
//     self.tabBarController.tabBar.hidden = YES;
    [self createView];
    [self getData];

}

- (void)createView
{
    [self createCollectionView];
    [self createLabel];
}

- (void)getData
{
    
    NSString *str = [[self.photosetID substringFromIndex:4] stringByReplacingOccurrencesOfString:@"|" withString:@"/"];
    str = [NSString stringWithFormat:@"http://c.m.163.com/photo/api/set/%@.json", str];
    NSLog(@"^^^^^^^%@", str);
    [xwzAFNetTool getNetWithURL:str body:nil headFile:nil responStyle:XWZ_JSON success:^(NSURLSessionDataTask *task, id responseObject) {
        
        
        NSMutableDictionary *dic = responseObject;
        self.photoArr = [NSMutableArray array];
        
        for (NSMutableDictionary *tempDic in [dic objectForKey:@"photos"]) {
            photoModel *model = [[photoModel alloc] initWithDic:tempDic];
            [self.photoArr addObject:model];
        }
        NSLog(@"%@", self.photoArr);
        [self.collectionV reloadData];
        self.label.text = [[self.photoArr objectAtIndex:0] note];
        
        
    } failure:^(NSURLSessionDataTask *task, NSError *error) {
        
        
    }];
    
    
    
}

- (void)createCollectionView
{
    UICollectionViewFlowLayout *flow = [[UICollectionViewFlowLayout alloc] init];
    flow.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    flow.itemSize = CGSizeMake(self.view.frame.size.width, self.view.frame.size.height);
    flow.minimumLineSpacing = 0;
    flow.minimumInteritemSpacing = 0;
    self.collectionV = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height) collectionViewLayout:flow];
    self.collectionV.backgroundColor = [UIColor clearColor];
    self.collectionV.delegate = self;
    self.collectionV.dataSource = self;
    self.collectionV.pagingEnabled = YES;
    [self.view addSubview:self.collectionV];
    [self.collectionV registerClass:[PhotoCollectionViewCell class] forCellWithReuseIdentifier:@"cell"];

}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return self.photoArr.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    PhotoCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath];
    photoModel *model = [self.photoArr objectAtIndex:indexPath.row];
    [cell.imageView sd_setImageWithURLStr:model.imgurl placeholderImage:[UIImage imageNamed:@"lol-tupian"]];
   
//    NSLog(@"%@", model.simgurl);
    return cell;
}

- (void)createLabel
{
    self.label = [[UILabel alloc] initWithFrame:CGRectMake(5, SCREEN_HEIGHT / 1.5 + 30, self.view.frame.size.width - 10, SCREEN_HEIGHT - SCREEN_HEIGHT / 1.3)];
//    self.label.backgroundColor = [UIColor whiteColor];
    self.label.numberOfLines = 0;
    self.label.textColor = [UIColor whiteColor];
    [self.view addSubview:self.label];
}

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    NSInteger page = self.collectionV.contentOffset.x / self.view.frame.size.width;
    self.label.text = [[self.photoArr objectAtIndex:page] note];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
